package com.example.mytestdemo.softkey

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.example.mytestdemo.R
import com.example.mytestdemo.utils.ScreenUtils

/**
 * @author zhiqiangRuan
 * @ClassName
 * @Date 2022/4/24
 */
class StudentAnswerView(private val mContext: Context) : FrameLayout(
    mContext
) {
    private lateinit var mContainerView: LinearLayout
    private var mIsSoftKeyBoard = false
    private val mScreenWidth: Int
    private val mScreenHeight: Int
    private lateinit var mFrameLayoutParams: FrameLayout.LayoutParams
    private var mLastBottomMargin = 0
    private var dragMoveHelper: DragMoveHelper? = null

    /**
     * 初始化布局
     */
    private fun initView() {
        LayoutInflater.from(mContext)
            .inflate(R.layout.new_quick_ask_student_dialog_answer_layout, this)
        mContainerView =
            findViewById<LinearLayout>(R.id.quick_ask_student_answer_dialog_container_ll)
        initContainerView()
        setSoftKeyBoardListener()
        dragMoveHelper = DragMoveHelper(mContainerView, this)
        dragMoveHelper!!.bind()
    }

    private fun initContainerView() {
        mFrameLayoutParams = FrameLayout.LayoutParams(
            500,
           200
        )
        mFrameLayoutParams.gravity = Gravity.BOTTOM or Gravity.END
        mFrameLayoutParams.bottomMargin = 28
        mFrameLayoutParams.rightMargin = 20
        mLastBottomMargin = mFrameLayoutParams.bottomMargin
        mContainerView.setLayoutParams(mFrameLayoutParams)
    }
    private fun setSoftKeyBoardListener() {
        SoftKeyBoardListener.setListener(
            (mContext as Activity).getWindow(),
            object : SoftKeyBoardListener.OnSoftKeyBoardChangeListener {
                override fun keyBoardShow(height: Int) {
                    mIsSoftKeyBoard = true
                    changePosition(height)
                }

                override fun keyBoardHide(height: Int) {
                    mIsSoftKeyBoard = false
                    mFrameLayoutParams.bottomMargin = mLastBottomMargin
                    mContainerView.setLayoutParams(mFrameLayoutParams)
                }
            })
    }

    /**
     * 软键盘弹出时设置弹框位置
     *
     * @param height 软键盘高度
     */
    fun changePosition(height: Int) {}

    init {
        mScreenWidth = ScreenUtils.getScreenWidth(mContext)
        mScreenHeight = ScreenUtils.getScreenHeight(mContext)
        initView()
    }
}